uniform sampler2D tex;
//xoffset yoffset
uniform float h_off, w_off ;
void main()
{

	//Get three vertex texcoord
	vec4 vertex1 = texture2D (tex, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + 1.0f * h_off));
	vec4 vertex2 = texture2D (tex, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + 2.0f * h_off));
	vec4 vertex3 = texture2D (tex, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + 3.0f * h_off));
	
	vec4 color1 = texture2D (tex, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + 4.0f * h_off));
	vec4 color2 = texture2D (tex, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + 5.0f * h_off));
	vec4 color3 = texture2D (tex, vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t + 6.0f * h_off));
	
	vec2 texcoord = gl_TexCoord[0].st ;
	vec2 v1coord = vec2(vertex1.x, vertex1.y) ; 
	vec2 v2coord = vec2(vertex2.x, vertex2.y) ; 
	vec2 v3coord = vec2(vertex3.x, vertex3.y) ;

	float dis1 = distance (texcoord, v1coord); 
	float dis2 = distance (texcoord, v2coord); 
	float dis3 = distance (texcoord, v3coord); 

	vec4 color ;
	if (dis1 <= dis2 && dis1 < dis3)
	{
		color = color1;
	}
	else if (dis2 < dis1 && dis2 <= dis3)
	{
		color = color2 ;
	}
	else
	{
		color = color3; 
	}


//	vec2 texcoord = vec2 (gl_TexCoord[0].s, gl_TexCoord[0].t );
//	vec4 color = texture2D(tex, texcoord);

	gl_FragColor = color;
}
